package p2.code;

/**
 * 竞态条件学习
 */
public class RaceCondition {

    private static Integer num1 = 0;
    private static Integer num2 = 0;

    /**
     * 下面的代码存在竞态条件
     * @param num
     */
    public static void add(int num) {
        num1 += num;
        num2 -= num;
    }

    public static void main(String[] args) throws InterruptedException {
        for(int i=0;i<500;i++){
            new Thread(()->{
                RaceCondition.add(1);
            }).start();
        }
        Thread.sleep(10000);
        System.out.println("num1: "+num1);
        System.out.println("num2: "+num2);
    }

}
